package com.threeti.mecool.web.controller.report;

import com.threeti.mecool.core.application.acl.UserService;
import com.threeti.mecool.core.domain.model.acl.User;
import com.threeti.mecool.core.domain.model.project.SellingUpload;
import com.threeti.mecool.framework.util.PageHandler;
import com.threeti.mecool.legacy.domain.model.basicdata.biz.PosKindLegacy;
import com.threeti.mecool.legacy.domain.model.basicdata.biz.PosSysTypeLegacy;
import com.threeti.mecool.legacy.infrastructure.persistence.AdminDivisionLegacyRepository;
import com.threeti.mecool.legacy.infrastructure.persistence.PosKindLegacyRepository;
import com.threeti.mecool.legacy.infrastructure.persistence.PosSysTypeLegacyRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/report/sellingUpload")
public class SellingUploadReportController {
    @Autowired PosKindLegacyRepository posKindLegacyRepository;
    @Autowired PosSysTypeLegacyRepository posSysTypeLegacyRepository;
    @Autowired UserService userService;
    @Autowired SellingUpload sellingUpload;
    @Autowired AdminDivisionLegacyRepository adminDivisionLegacyRepository;

    @RequestMapping("/sellingUploadList.html")
    public void sellingUploadList(Map<String, Object> modelMap) {
        List<PosKindLegacy> channelTypeList = posKindLegacyRepository.findAll(new Sort("name"));
        List<PosSysTypeLegacy> channelSystemList = posSysTypeLegacyRepository.findAll(new Sort("name"));
        modelMap.put("channelTypeList", channelTypeList);
        modelMap.put("channelSystemList", channelSystemList);
        modelMap.put("provinceList", adminDivisionLegacyRepository.findByDeep(1));
    }

    @RequestMapping("/sellingUploadListData.html")
    public void sellingUploadListData(String projectCode, String projectName, String channelCode, String channelName,
                                      String adCode, String channelType, String channelSystem,
                                      @DateTimeFormat(pattern = "yyyy-MM-dd") Date dateFrom,
                                      @DateTimeFormat(pattern = "yyyy-MM-dd") Date dateTo,
                                      Pageable pageable, Map<String, Object> model) {
        User user = userService.getCurrentUserWithPermissionRoleAndEntries();
        Page page = sellingUpload.findSellingUpload(pageable, user,
                projectCode, projectName, channelCode, channelName,
                adCode, channelType, channelSystem, dateFrom, dateTo);
        PageHandler pageHandler = new PageHandler(pageable.getPageNumber(), (int) page.getTotalElements(), pageable.getPageSize(), page.getContent());
        model.put("page", pageHandler);
    }
}
